import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const routes = [{
        path: '/login',
        component: () =>
            import ('@/views/login/index'),
        hidden: true
    },
    {
        path: '/404',
        component: () =>
            import ('@/views/404'),
        hidden: true
    },
    {
        path: '/',
        redirect: '/home/homeIndex'
    },
    {
        path: '/home',
        redirect: 'homeIndex',
        name: 'home',
        component: () =>
            import ('@/layout/index'),
        children: [{
            path: 'homeIndex',
            name: 'homeIndex',
            component: () =>
                import ('@/views/index/index'),
            meta: { title: '首页', icon: 'home' }
        }]
    },
    {
        path: '/accountManager',
        redirect: 'adminManager',
        name: 'accountManager',
        meta: { title: '账号管理', icon: 'accountManager' },
        component: () =>
            import ('@/layout/index'),
        children: [{
            path: 'adminManager',
            name: 'adminManager',
            component: () =>
                import ('@/views/adminManager/index'),
            meta: { title: '管理员', icon: 'accountAdmin' }
        }, {
            path: 'userManager',
            name: 'userManager',
            component: () =>
                import ('@/views/userManager/index'),
            meta: { title: '用户管理', icon: 'accountUser' }
        }]
    },
    {
        path: '/systemManager',
        redirect: 'jobManager',
        name: 'systemManager',
        meta: { title: '测评管理', icon: 'system' },
        component: () =>
            import ('@/layout/index'),
        children: [{
            path: 'jobManager',
            name: 'jobManager',
            component: () =>
                import ('@/views/jobManager/index'),
            meta: { title: '岗位管理', icon: 'job' }
        }, {
            path: 'dimensionManager',
            name: 'dimensionManager',
            component: () =>
                import ('@/views/dimensionManager/index'),
            meta: { title: '维度管理', icon: 'dimension' }
        }, {
            path: 'scaleManager',
            name: 'scaleManager',
            component: () =>
                import ('@/views/scaleManager/index'),
            meta: { title: '考卷管理', icon: 'scale' }
        }]
    },
    {
        path: '/questionManager',
        redirect: 'commonQuestion',
        name: 'questionManager',
        meta: { title: '题库管理', icon: 'test' },
        component: () =>
            import ('@/layout/index'),
        children: [{
            path: 'commonQuestion',
            name: 'commonQuestion',
            component: () =>
                import ('@/views/commonQuestion/index'),
            meta: { title: '共性题库', icon: 'test' }
        }, {
            path: 'personalityQuestion',
            name: 'personalityQuestion',
            component: () =>
                import ('@/views/personalityQuestion/index'),
            meta: { title: '个性题库', icon: 'test' }
        }]
    }
]

const blackList = [

]

const router = new VueRouter({
    mode: 'history',
    base: process.env.BASE_URL,
    routes,
    scrollBehavior(to, from, savedPosition) { //跳转路由回到顶部
        for (let index = 0; index < blackList.length; index++) {
            const element = blackList[index];
            if (to.path.search(element) >= 0) {
                return;
            }
        }
        return { x: 0, y: 0 }
    }
})

const createRouter = () => new VueRouter({
    mode: 'history',
    base: process.env.BASE_URL,
    routes,
    scrollBehavior(to, from, savedPosition) { //跳转路由回到顶部
        for (let index = 0; index < blackList.length; index++) {
            const element = blackList[index];
            if (to.path.search(element) >= 0) {
                return;
            }
        }
        return { x: 0, y: 0 }
    }
})

export function resetRouter() {
    const newRouter = createRouter()
    router.matcher = newRouter.matcher // reset router
}

export default router